package com.rop.znyx.product.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rop.znyx.common.result.Result;
import com.rop.znyx.enums.SkuType;
import com.rop.znyx.model.product.Category;
import com.rop.znyx.model.product.SkuInfo;
import com.rop.znyx.product.service.CategoryService;
import com.rop.znyx.product.service.SkuInfoService;
import com.rop.znyx.vo.product.SkuInfoVo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * Created with IntelliJ IDEA
 * User: Rop
 * Date: 2023/6/30
 * Time: 14:44
 * Description:
 */

@RestController
@RequestMapping("api/product/inner")
@RequiredArgsConstructor
public class ProductInnerController {

    private final CategoryService categoryService;

    private final SkuInfoService skuInfoService;

    @ApiOperation("根据分类id获取分类信息")
    @GetMapping("getCategory/{categoryId}")
    public Result<Category> getCategory(@PathVariable Long categoryId) {
        Category category = categoryService.getById(categoryId);
        return Result.ok(category);
    }

    @ApiOperation("根据skuId获取sku信息")
    @GetMapping("getSkuInfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable Long skuId) {
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return Result.ok(skuInfo);
    }

    @ApiOperation("根据skuId集合获取sku信息")
    @PostMapping("findSkuInfoList")
    public Result<List<SkuInfo>> findSkuInfoList(@RequestBody List<Long> skuIds) {
        List<SkuInfo> skuInfos = skuInfoService.getSkuInfoByIds(skuIds);
        return Result.ok(skuInfos);
    }

    /**
     * 根据关键字获取sku列表
     */
    @GetMapping("findSkuInfoByKeyword/{keyword}")
    public Result<List<SkuInfo>> findSkuInfoByKeyword(@PathVariable("keyword") String keyword) {
        return Result.ok(skuInfoService.findSkuInfoByKeyword(keyword));
    }

    /**
     * 批量获取分类信息
     */
    @PostMapping("findCategoryList")
    public Result<List<Category>> findCategoryList(@RequestBody List<Long> categoryIdList) {
        return Result.ok(categoryService.listByIds(categoryIdList));
    }

    /**
     * 更新秒杀商品
     */
    @PostMapping("updateSkuType")
    public void updateSkuType(@RequestParam List<Long> skuIdList, @RequestParam SkuType skuType) {
        skuInfoService.updateSkuType(skuIdList, skuType);
    }

    /**
     * 获取所有sku分类
     */
    @GetMapping("findAllCategoryList")
    public Result<List<Category>> findAllCategoryList() {
        List<Category> categories = categoryService.list();
        return Result.ok(categories);
    }

    /**
     * 获取新人专享
     */
    @GetMapping("findNewPersonSkuInfoList")
    public Result<List<SkuInfo>> findNewPersonSkuInfoList() {
        // 获取新人专享且上架 且仅显示其中三个
        List<SkuInfo> skuInfoList = skuInfoService.list(new LambdaQueryWrapper<SkuInfo>()
                .eq(SkuInfo::getIsNewPerson, 1)
                .eq(SkuInfo::getPublishStatus, 1)
                .orderByDesc(SkuInfo::getStock)
                .last("limit 3"));

        return Result.ok(skuInfoList);
    }

    @ApiOperation(value = "根据skuId获取sku信息")
    @GetMapping("getSkuInfoVo/{skuId}")
    public SkuInfoVo getSkuInfoVo(@PathVariable("skuId") Long skuId) {
        return skuInfoService.getSkuInfoById(skuId);
    }
}
